package com.huiyang.demo.entity;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.mikephil.charting.data.Entry;
import com.huiyang.demo.utils.BCGArrayList;


/**
 * @ProjectName: DEEBCG Websoket Demo
 * @Package: com.clock.blelib.entity
 * @ClassName: DeviceInfoEntity
 * @Description: java类作用描述
 * @Author: yuan
 * @CreateDate: 2020/11/3 8:29 AM
 */
public class DeviceInfoEntity {

    private String MsgType;//消息类型
    private String Name;//设备编号
    private String DeviceMac;//设备编号
    private String Temperature;//温度  14.1℃
    private String BedStatus;//在床状态
    private String BreatheRate;//呼吸频率  13次/分钟
    private String HeartRateInstant;//即时呼吸频率  13次/分钟
    private String BreatheRateStable;//呼吸稳定度
    private String HeartRate;//平均心跳频率  76次/分钟
    private String showHeartRate;//显示的心跳频率  76次/分钟
    private String HeartRateStable;//心跳稳定度
    private String SnoreRate;//打鼾频率  6次/分钟
    private String SnoreTime;//打鼾时间  yyyy-MM-ddTHH:mm:ss
    private BCGArrayList ecgList;

    private String HeartRateShow;//心跳最终显示
    private boolean isStartHeartFilter;//是否启动心跳报警判断
    private boolean isStartBreatheFilter;//是否启动呼吸报警判断


    static DeviceInfoEntity entity = null;
    static JSONObject object;
    static String MsgTypeStr;
    public static DeviceInfoEntity parseData(String json) {
        entity = null;
        object = JSON.parseObject(json);
        MsgTypeStr = object.getString("MsgType");
        if (MsgTypeStr.equals("DeviceInfo")) {//实时状态数据
            entity = new DeviceInfoEntity();
            entity.setMsgType(MsgTypeStr);
            entity.setName(object.getString("Name"));
            entity.setDeviceMac(object.getString("DeviceMac"));
            entity.setTemperature(object.getString("Temperature"));
            entity.setBedStatus(object.getString("BedStatus"));
            entity.setBreatheRate(object.getString("BreatheRate"));
            entity.setBreatheRateStable(getBreatheStable(object.getString("BreatheRateStable")));
            entity.setHeartRate(object.getString("HeartRate"));
            entity.setHeartRateInstant(object.getString("HeartRateInstant"));
            entity.setHeartRateStable(getHeartStable(object.getString("HeartRateStable")));
            String snore = object.getString("SnoreRate");
            entity.setSnoreRate(TextUtils.isEmpty(snore) ? "无" : snore);
            entity.setSnoreTime(object.getString("SnoreTime"));
        }else if (MsgTypeStr.equals("PingMessage")){
            entity = new DeviceInfoEntity();
            entity.setMsgType(MsgTypeStr);
        }else if (MsgTypeStr.equals("ChartData")){//实时波形数据
            entity = new DeviceInfoEntity();
            entity.setMsgType(MsgTypeStr);
            entity.setDeviceMac(object.getString("DeviceMac"));
            JSONArray jsarr = object.getJSONArray("Ints");
            BCGArrayList cacheList = new BCGArrayList();
            for (int i = 0;i<jsarr.size();i++){
                JSONArray arr2 = jsarr.getJSONArray(i);
                cacheList.add(new Entry(arr2.getFloatValue(0),arr2.getFloatValue(1)));
            }
            entity.setEcgList(cacheList);

        }
        return entity;
    }

    private static String getBreatheStable(String breatheState) {
        String stable = "";
        // 0-9：很平稳；10-14：平稳；15-24：一般；25-35不平稳；35及以上：非常不平稳
        if (TextUtils.isEmpty(breatheState) || breatheState.equals("----")) {
            stable = "--";
        } else {
            float s = Float.parseFloat(breatheState);
            if (0 <= s && s <= 9) {
                stable = "很平稳";
            } else if (10 <= s && s <= 14) {
                stable = "平稳";
            } else if (15 <= s && s <= 24) {
                stable = "一般";
            } else if (25 <= s && s < 35) {
                stable = "不平稳";
            } else if (35 <= s) {
                stable = "非常不平稳";
            }
        }

        return stable;
    }

    private static String getHeartStable(String breatheState) {
        String stable = "";
        // 0-14：很平稳；15-24：平稳；25-34：一般；35-45不平稳
        if (TextUtils.isEmpty(breatheState) || breatheState.equals("----")) {
            stable = "--";
        } else {
            float s = Float.parseFloat(breatheState);
            if (0 <= s && s <= 14) {
                stable = "很平稳";
            } else if (15 <= s && s <= 24) {
                stable = "平稳";
            } else if (25 <= s && s <= 34) {
                stable = "一般";
            } else if (35 <= s && s <= 45) {
                stable = "不平稳";
            }
        }

        return stable;
    }

    public DeviceInfoEntity() {
    }

    public String getMsgType() {
        return MsgType;
    }

    public void setMsgType(String msgType) {
        MsgType = msgType;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getHeartRateInstant() {
        return HeartRateInstant;
    }

    public void setHeartRateInstant(String heartRateInstant) {
        HeartRateInstant = heartRateInstant;
    }
    public String getDeviceMac() {
        return DeviceMac;
    }

    public void setDeviceMac(String deviceMac) {
        DeviceMac = deviceMac;
    }

    public String getTemperature() {
        return Temperature;
    }

    public void setTemperature(String temperature) {
        Temperature = temperature;
    }

    public String getBedStatus() {
        return BedStatus;
    }

    public void setBedStatus(String bedStatus) {
        BedStatus = bedStatus;
    }

    public String getBreatheRate() {
        return BreatheRate;
    }

    public void setBreatheRate(String breatheRate) {
        BreatheRate = breatheRate;
    }

    public String getBreatheRateStable() {
        return BreatheRateStable;
    }

    public void setBreatheRateStable(String breatheRateStable) {
        BreatheRateStable = breatheRateStable;
    }

    public String getHeartRate() {
        return HeartRate;
    }

    public void setHeartRate(String heartRate) {
        HeartRate = heartRate;
    }

    public String getHeartRateStable() {
        return HeartRateStable;
    }

    public void setHeartRateStable(String heartRateStable) {
        HeartRateStable = heartRateStable;
    }

    public String getSnoreRate() {
        return SnoreRate;
    }

    public void setSnoreRate(String snoreRate) {
        SnoreRate = snoreRate;
    }

    public String getSnoreTime() {
        return SnoreTime;
    }

    public void setSnoreTime(String snoreTime) {
        SnoreTime = snoreTime;
    }

    public String getShowHeartRate() {
        return showHeartRate;
    }

    public void setShowHeartRate(String showHeartRate) {
        this.showHeartRate = showHeartRate;
    }

    public String getHeartRateShow() {
        return HeartRateShow;
    }

    public void setHeartRateShow(String heartRateShow) {
        HeartRateShow = heartRateShow;
    }

    public BCGArrayList getEcgList() {
        return ecgList;
    }

    public void setEcgList(BCGArrayList ecgList) {
        this.ecgList = ecgList;
    }

    public boolean isStartHeartFilter() {
        return isStartHeartFilter;
    }

    public void setStartHeartFilter(boolean startHeartFilter) {
        isStartHeartFilter = startHeartFilter;
    }

    public boolean isStartBreatheFilter() {
        return isStartBreatheFilter;
    }

    public void setStartBreatheFilter(boolean startBreatheFilter) {
        isStartBreatheFilter = startBreatheFilter;
    }
}
